package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;

import com.example.demo.adapter.GoodsAdapter;
import com.example.demo.entity.Goods;

import java.util.ArrayList;
import java.util.List;

public class GoodsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private Context context;
    private List<Goods> goodsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);

        recyclerView = findViewById(R.id.recycler_view1);
        context = this;

        //1.
        RecyclerView.LayoutManager  manager = new GridLayoutManager(context, 2);
        //设置滚动方向
//        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
//        layoutManager.setOrientation(RecyclerView.HORIZONTAL);

        recyclerView.setLayoutManager(manager);


        //2.
        GoodsAdapter adapter = new GoodsAdapter(this);
        recyclerView.setAdapter(adapter);

        initData();
        adapter.setData(goodsList);
    }

    private void initData(){

        for (int i = 0; i < 50; i++){
            Goods goods = new Goods();
            goods.setImg(i % 2 == 0 ? R.drawable.a3 : R.drawable.a4);
            if (i % 2 == 0){
                goods.setName("三体：黑暗森林");
                goods.setCount("30万");
                goods.setA("96%");
            }
            else {
                goods.setName("百年孤独");
                goods.setCount("30万");
                goods.setA("96%");
            }

            goodsList.add(goods);
        }
    }
}